#include "vki.h"
#include "funcset.h"
#include "datapicker.h"
#include "modelcreate.h"


#define MAX_ELEM 11
#define MAX_NODE 22
static Vint nid[MAX_NODE] = {
	1, 2,30,40,
	5, 6,70,80, 
	9,10,11,
	12,13,14,
	15,16,17,18,19,20,21,22 };

static Vint conn[MAX_ELEM][8] = {
		1, 2, 6, 5, 9,10,13,12,
		2,30,70, 6,10,11,14,13,
		30,40,80,70, 0, 0, 0, 0,
		40,15,80, 0, 0, 0, 0, 0,
		9,10,13,16, 0, 0, 0, 0,
		10,13,16,11,14,17, 0, 0,
		15,18, 0, 0, 0, 0, 0, 0,
		18, 0, 0, 0, 0, 0, 0, 0,
		18,19, 0, 0, 0, 0, 0, 0,
		18,20, 0, 0, 0, 0, 0, 0,
		15,21,22, 0, 0, 0, 0, 0 };

static Vint shap[MAX_ELEM] = {
			VIS_SHAPEHEX, VIS_SHAPEHEX, VIS_SHAPEQUAD, VIS_SHAPETRI,
			VIS_SHAPETET, VIS_SHAPEWED, VIS_SHAPELINE, VIS_SHAPEPOINT,
			VIS_SHAPELINE, VIS_SHAPEPOINT, VIS_SHAPEPOINT };

static Vfloat coords[MAX_NODE][3] = {
	0.,0.,0.,  1.,0.,0.,  2.,0.,1.,  3.,0.,1., 
	0.,1.,0.,  1.,1.,0.,  2.,1.,1.,  3.,1.,1., 
	0.,0.,2.,  1.,0.,2.,  2.,0.,2.,
	0.,1.,2.,  1.,1.,2.,  2.,1.,2.,
	4.,0.,1.,  1.,0.,3.,  2.,0.,3., 5.,0.,1., 6.,-1.,1.,
	5.,1.,1.,  4.,-1.,1., 4.5,-1.,1.  };

static Vint maxi[MAX_ELEM] = {
					0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3 };


int mergedata();
void main(int argc,char ** argv)
{
// 
// 	DataPicker dp("2.op2");
// 	dp.printLibDataset();
//  
// 	dp.testFunc();
// 
// 	vdm_Dataset* dataset;
// 	dp.getDatasetFromName("NID.N",&dataset);
// 	Vint numattributes;
// 	vdm_DatasetGetNumAttributes (dataset,&numattributes);
// 	dp.transformToNasInFile();
// 
//   
// 	
// 	return;
 
	
	
	Material mat;
	mat.addValue(MPROP_E,1.e+8);
	mat.addValue(MPROP_DENSITY,.1);
	mat.addValue(MPROP_NU,.77);
	 

	ModelCreate mc(MAX_NODE,MAX_ELEM);
	VKITopo topo;
	for(int i = 0; i < MAX_ELEM; i++) 
	{
		
		topo.shape = shap[i];
		topo.maxi = maxi[i];
		topo.maxj = 0;
		topo.maxk = 0;
		mc.addTopology(&topo,i+1);
	 
	}

	for(int i = 0; i < MAX_ELEM; i++)
	{
		mc.addElemNode(conn[i],i+1);
 
	}
 
	for(int i = 0; i < MAX_NODE; i++)
	{
		mc.addCoords (coords[i],nid[i]);
	}

	mc.addMaterial(&mat);
	
	Material mat2;
	mat2.addValue(MPROP_E,2.e+8);
	mat2.addValue(MPROP_DENSITY,.2);
	mat2.addValue(MPROP_NU,.55);
	mc.addMaterial(&mat2);

	Material mat3;
	mat3.addValue(MPROP_E,3.e+8);
	mat3.addValue(MPROP_DENSITY,.3);
	mat3.addValue(MPROP_NU,.88);
	mc.addMaterial(&mat3);

	LoadCase lc;
	float f[3];
	f[0] = 100.;
	f[1] = 0.;
	f[2] = 0.;

	lc.setConc(13,LCASE_FORCE,f);
	f[2] = 100;
	lc.setConc(5,LCASE_FORCE,f);
	f[2] = 100;
	lc.setConc(19,LCASE_FORCE,f);

	LoadCase lc2;
	float v[6];
	v[0] = 50.;
	v[1] = 20.;
	v[2] = 30.;
	v[3] = 60.;
	v[4] = 50.;
	v[5] = 80.;
	 
	lc2.setDist(mc.gridFun,SYS_FACE,7,1,LCASE_PRES,v);

	mc.addLoadCase(&lc);
	mc.addLoadCase(&lc2);


ElemProp ep;


	mc.writeToFile();

	return;

	{
		Vint i, j, k;
		Vint iparams[1];
		Vdouble dparams[4];
		vis_EProp *eprop;
		Vint ntypes, type[EPROP_MAX];
		Vint nval, nloc, dtyp;
		Vchar name[64];
		Vdouble cthick[4], thick[8];

		/* create eprop for shell element properties */ 
		eprop = vis_EPropBegin ();
		vis_EPropDef (eprop,VIS_ELEM_SHELL);

		/* set material id */ 
		vis_EPropSetValuei (eprop,EPROP_MID,1);

		/* install corner thicknesses */ 
		cthick[0] = .5;
		cthick[1] = .5;
		cthick[2] = 1.5;
		cthick[3] = 1.5;
		vis_EPropSetValuedv (eprop,EPROP_THICKNESS,cthick);

		/* compute thicknesses for 8 node shell */ 
		vis_EPropEvaldv (eprop,EPROP_THICKNESS,VIS_SHAPEQUAD,3,0,thick);
		printf("\nElement node thickness:\n");
		for(i = 0; i < 8; i++) {
			printf("thickness= %e\n",thick[i]);
		}
		/* traverse defined element properties */ 
		vis_EPropValueType (eprop,&ntypes,type);
		printf("ntypes = %d\n",ntypes);

		/* loop through types */ 
		for(i = 0; i < ntypes; i++) {
			vis_EPropValueName (eprop,type[i],name);
			printf("\n name = %s\n",name);
			vis_EPropValueParams (eprop,type[i],&nval,&nloc,&dtyp);
			printf(" nval= %d, nloc= %d\n",nval,nloc);

			/* integer valued */ 
			if(dtyp == SYS_INTEGER) {
				printf(" Integer\n");
				vis_EPropValueInteger (eprop,type[i],iparams);
				for(j = 0; j < nval; j++) {
					printf("  value= %d\n",iparams[j]);
				}
				/* real valued */ 
			} else {
				vis_EPropValueDouble (eprop,type[i],dparams);
				printf(" Real\n");
				for(k = 0; k < nloc; k++) {
					printf(" Location= %d\n",k);
					for(j = 0; j < nval; j++) {
						printf("  value= %e\n",dparams[k*nval+j]);
					}
				}
			}
		}
		/* end objects */ 
		vis_EPropEnd (eprop);


	}


/*
//testdatapicker
	mergedata( );
	DataPicker dp("EMT_StruInput.dat");
	long  maxNode = dp.getMaxNode();
  

	vis_State *state = vis_StateBegin ();
	vis_StateDef (state,maxNode,SYS_NODE,SYS_NONE,SYS_SCALAR);
	vis_StateSetObject (state,VIS_GRIDFUN,dp.gridfun);

	FILE *pf = fopen("EMT_MapDataOutput.dat","r");
	if (!pf)
	{
		printf("err read file!\n");
		return ;
	}
	char ch[1025]={0};
 
	while (	 fgets(ch,1024,pf))
	{
		char header[9] = {0};
		int flag = 2;

		int nid1 = 0;
		float f1 = 0;
		int nid2 = 0;
		float f2 = 0;
		int nid3 = 0;
		float f3 = 0;
		int slen= strlen(ch);

		if (strncmp(ch,"TEMP ",5) == 0 )
		{

			sscanf(ch,"%s %d %d %f %d %f %d %f",
				&header,&flag,&nid1,&f1,&nid2,&f2,&nid3,&f3);
			if (slen >=32)
			{
				vis_StateSetData (state,nid1,&f1);
			}
			if (slen >=48)
			{
				vis_StateSetData (state,nid2,&f2);
			}
			if (slen >=64)
			{
				vis_StateSetData (state,nid3,&f3);
			}

		}

	}
	fclose(pf);

	vis_RProp *rprop = vis_RPropBegin();
	vis_RPropDef (rprop,SYS_NODE,SYS_NONE);
	vis_RPropSetIds(rprop,1,0,0);
	vis_RPropSetValuec (rprop,RPROP_TITLE,"xxxxxxXXX");
	vis_RPropSetType(rprop,SYS_RES_TEMP);

 
 
	dp.transformToNasOutFile(state,rprop);

 
	vis_StateEnd(state);
	vis_RPropEnd (rprop);

	*/
 	 
/*
	dp.printLManDatasetToFile("aa.txt");

	dp.printLManDataset();

	dp.printLibDataset();

	dp.travelLibDataset();
	vdm_Dataset *ds;
	dp.getDatasetFromName("D.N*",&ds);
	Vlong a;
	Vint aa,b,c,d;
	Vchar name[42];
	vdm_DatasetInq(ds,name,&a,&b,&c,&d);

	vis_State *state;
	state = vis_StateBegin();

	dp.getStateFromDatasetName("D.N*",state,2);
 	dp.getDisplacementMinMax(state);

	dp.getStateFromDatasetName("S.E*",state,3);
 	dp.getMisesStressAtElemMinMax(state);
	dp.getMinPrincipalStressAtElemMinMax(state);
	dp.getMaxPrincipalStressAtElemMinMax(state);

	dp.getStateFromDatasetName("S.EL*",state,1);
 	dp.getMisesStressMinMax(state);
	dp.getMinPrincipalStressMinMax(state);
	dp.getMaxPrincipalStressMinMax(state);

	vis_StateEnd(state);

	*/ 
}

